//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.dao
{
	import __AS3__.vec.Vector;
	
	import flash.data.SQLConnection;
	import flash.data.SQLResult;
	
	import reader.model.vo.ServiceVO;
	import reader.model.vo.UserVO;
	
	public class ServiceDAO extends BaseDAO implements IServiceDAO
	{
		//////////////////////////////////////////////////////////////////////////////
		//SQLStatement queries
		
		private static const INSERT_SERVICE:String = "INSERT INTO service (userId, type, name, link, key, secret, username, password, postid, authhead, authparams, login_url, login_flag, login_width, login_height, endpoint, destination, needAlert, config, isGetAlbum, isPostAlbum, isPostBlog, isEditBlog, isSubscribe) VALUES(:userId, :type, :name, :link, :key, :secret, :username, :password, :postid, :authhead, :authparams, :login_url, :login_flag, :login_width, :login_height, :endpoint, :destination, :needAlert, :config, :isGetAlbum, :isPostAlbum, :isPostBlog, :isEditBlog, :isSubscribe)";
		private static const DELETE_SERVICE:String = "DELETE FROM service WHERE sid=:sid";
		private static const DELETE_SERVICE_BY_USER:String = "DELETE FROM service WHERE userId=:userId";
		private static const UPDATE_SERVICE:String = "UPDATE service SET username=:username, password=:password, postid=:postid, authparams=:authparams WHERE sid=:sid";
		private static const SELETE_SERVICE:String = "SELECT * FROM service WHERE sid=:sid";
		private static const SELETE_ALL_SERVICE:String = "SELECT * FROM service WHERE userId=:userId";
		
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function ServiceDAO(connection:SQLConnection)
		{
			super(connection);
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		//----------------------------------------------------------------------------
		//
		// INSERT
		//
		//----------------------------------------------------------------------------
		
		public function insertService(service:ServiceVO, user:UserVO):int
		{			
			var obj:Object = {};
			obj.type = service.type;
			obj.name = service.name;
			obj.link = service.link;
			obj.key = service.key;
			obj.secret = service.secret;
			obj.username = service.username;
			obj.password = service.password;
			obj.postid = service.postid;
			obj.authhead = service.authhead;
			obj.authparams = service.authparams;
			obj.login_url = service.login_url;
			obj.login_flag = service.login_flag;
			obj.login_width = service.login_width;
			obj.login_height = service.login_height;
			obj.endpoint = service.endpoint;
			obj.destination = service.destination;
			obj.needAlert = service.needAlert;
			obj.config = service.config;
			obj.isGetAlbum = service.isGetAlbum;
			obj.isPostAlbum = service.isPostAlbum;
			obj.isSubscribe = service.isSubscribe;
			obj.isPostBlog = service.isPostBlog;
			obj.isEditBlog = service.isEditBlog;
			obj.userId = user.userId;
			var sqlResult:SQLResult = execute(INSERT_SERVICE, obj);
            return int(sqlResult.lastInsertRowID);
		}
		
		//----------------------------------------------------------------------------
		//
		// UPDATE
		//
		//----------------------------------------------------------------------------
		
		public function updateService(service:ServiceVO):Boolean
		{
			return update(UPDATE_SERVICE, {username: service.username, password: service.password, postid: service.postid, authparams: service.authparams, sid: service.sid});
		}
		
		//----------------------------------------------------------------------------
		//
		// DELETE
		//
		//----------------------------------------------------------------------------
		
		public function deleteService(service:ServiceVO):Boolean
		{
            return drop(DELETE_SERVICE, {s: service.sid});
		}
		
		public function deleteServiceByUser(user:UserVO):Boolean
		{
            return drop(DELETE_SERVICE_BY_USER, {userId: user.userId});
		}
		
		//----------------------------------------------------------------------------
		//
		// SELETE
		//
		//----------------------------------------------------------------------------
		
		public function selectService(sid:int):ServiceVO
		{
			return select(SELETE_SERVICE, {sid: sid})[0];
		}
		
		public function selectAllServices(user:UserVO):Vector.<ServiceVO>
		{
			return select(SELETE_ALL_SERVICE, {userId: user.userId});
		}
		
		private function select(query:String, data:Object):Vector.<ServiceVO>
		{
			var result:SQLResult = execute(query, data);
			if(!result || !result.data)
			{
				return null;
			}
			var i:int, l:int = result.data.length;
			var dataArray:Vector.<ServiceVO> = new Vector.<ServiceVO>();
						
			for each(var obj:Object in result.data)
			{				
				var _data:ServiceVO = new ServiceVO();
				_data.sid = obj["sid"];
				_data.userId = obj["userId"];
				_data.type = obj["type"];
				_data.name = obj["name"];
				_data.link = obj["link"];
				_data.key = obj["key"];
				_data.secret = obj["secret"];
				_data.username = obj["username"];
				_data.password = obj["password"];
				_data.postid = obj["postid"];
				_data.authhead = obj["authhead"];
				_data.authparams = obj["authparams"];
				_data.login_url = obj["login_url"];
				_data.login_flag = obj["login_flag"];
				_data.login_width = obj["login_width"];
				_data.login_height = obj["login_height"];
				_data.endpoint = obj["endpoint"];
				_data.destination = obj["destination"];
				_data.needAlert = obj["needAlert"];
				_data.isGetAlbum = obj["isGetAlbum"];
				_data.isPostAlbum = obj["isPostAlbum"];
				_data.isSubscribe = obj["isSubscribe"];
				_data.isPostBlog = obj["isPostBlog"];
				_data.isEditBlog = obj["isEditBlog"];
				_data.config = obj["config"];
				dataArray.push(_data);
			}
            return dataArray;
		}

	}
}